package com.bluegalaxy.sitescore.service.impl;

import com.bluegalaxy.sitescore.common.enums.SourceTypeSelect;
import com.bluegalaxy.sitescore.entity.SiteSource;
import com.bluegalaxy.sitescore.entity.TSiteSource;
import com.bluegalaxy.sitescore.entity.TbSiteSource;
import com.bluegalaxy.sitescore.mapper.TbSiteSourceMapper;
import com.bluegalaxy.sitescore.service.TbSiteSourceService;
import com.bluegalaxy.sitescore.service.support.BaseServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/23 0023.
 */
@Service
public class TbSiteSourceServiceImpl extends BaseServiceImpl<TbSiteSourceMapper, TbSiteSource> implements TbSiteSourceService {

    @Autowired
    private TbSiteSourceMapper siteSourceMapper;
    @Override
    public List<SiteSource> getTbSiteSource(Integer siteId) {
        List<TbSiteSource> siteSourceList = siteSourceMapper.SelectSiteSourceBySiteId(siteId);
        List<SiteSource> siteSources = new ArrayList<>();
        for (TbSiteSource tbSiteSource : siteSourceList){
            SiteSource source = new SiteSource();
            source.setId(tbSiteSource.getId());
            source.setSite_Id(tbSiteSource.getSite_Id());
            source.setSource_name(tbSiteSource.getSource_name());
            source.setHWQ(tbSiteSource.getHWQ());
            SourceTypeSelect[] selects = SourceTypeSelect.values();
            for (SourceTypeSelect select: selects){
                if(select.getValue()==tbSiteSource.getSource_type().intValue()){
                    source.setSource_type(select.getText());
                }
            }
            siteSources.add(source);
        }
        return siteSources;
    }

    @Override
    public TbSiteSource getTbSiteSourceById(int id) {
        TbSiteSource tbSiteSource = new TbSiteSource();
        tbSiteSource.setId(id);
        return siteSourceMapper.selectOne(tbSiteSource);
    }

    @Override
    public boolean caculateHWQ(TSiteSource siteSource) {
        TbSiteSource tbSiteSource = new TbSiteSource();
        tbSiteSource.setSite_Id(Integer.valueOf(siteSource.getSiteId()));
        tbSiteSource.setSource_name(siteSource.getSource_name());
        tbSiteSource.setSource_type(Integer.valueOf(siteSource.getSource_type()));
        boolean flag = true;
        float hwq = 0;
        if(!StringUtils.isBlank(siteSource.getQuantityA())){
            tbSiteSource.setQuantity1(Float.valueOf(siteSource.getQuantityA()));
            hwq = tbSiteSource.getQuantity1();
            flag = false;
        }
        if(!StringUtils.isBlank(siteSource.getQuantityB())){
            tbSiteSource.setQuantity2(Float.valueOf(siteSource.getQuantityB()));
            if(flag){
                hwq = tbSiteSource.getQuantity2()/5000;
                flag = false;
            }
        }
        if(!StringUtils.isBlank(siteSource.getQuantityC())){
            tbSiteSource.setQuantity3(Float.valueOf(siteSource.getQuantityC()));
            if(flag){
                int sourceType = tbSiteSource.getSource_type();
                if(sourceType==2||sourceType==7){
                    hwq = tbSiteSource.getQuantity3()/2500;
                }else if(sourceType!=9){
                    hwq = tbSiteSource.getQuantity3()/2.5f;
                }
                flag = false;
            }
        }
        if(!StringUtils.isBlank(siteSource.getQuantityD())){
            tbSiteSource.setQuantity4(Float.valueOf(siteSource.getQuantityD()));
            if(flag){
                int sourceType = tbSiteSource.getSource_type();
                if(sourceType==2){
                    hwq = tbSiteSource.getQuantity4()/3400;
                }else if(sourceType==3||sourceType==4||sourceType==8){
                    hwq = tbSiteSource.getQuantity4()/13;
                }else if(sourceType==9){
                    hwq = tbSiteSource.getQuantity4()/270;
                }else if(sourceType==7){
                    hwq = tbSiteSource.getQuantity4()/34000;
                }
                flag = false;
            }
        }

        tbSiteSource.setHWQ(hwq);
        // System.out.println(hwq);
        //判断该更新还是插入
        if(StringUtils.isBlank(siteSource.getSource_site_Id())){
           if(siteSourceMapper.insertSiteSource(tbSiteSource)){
               return true;
           }else {
               return false;
           }
        }else{
            tbSiteSource.setId(Integer.valueOf(siteSource.getSource_site_Id()));
            if(siteSourceMapper.updateSourceById(tbSiteSource)){
                return true;
            }else {
                return false;
            }
        }
    }

    @Override
    public boolean delTbSiteSource(int id) {
        return siteSourceMapper.delSourceById(id);
    }

    @Override
    public BigDecimal caculateScore(BigDecimal groundWater, BigDecimal surfaceWater, BigDecimal soil, BigDecimal air) {
        BigDecimal score = sqrt(groundWater.pow(2).add(surfaceWater.pow(2)).add(soil.pow(2)).add(air.pow(2)).divide(BigDecimal.valueOf(4)),2,BigDecimal.ROUND_DOWN);
        return score;
    }

    @Override
    public boolean isObservable(int sid) {
        //获取最大HWQ行的第一个选项，观察该选项是否有值
        List<String> quantity1List = siteSourceMapper.getQuantity1(sid);
        for (String quantity : quantity1List){
            //若有选项为空，则不可观察
            if (quantity==null){
                return false;
            }
        }
        return true;
    }

    @Override
    public  BigDecimal sqrt(BigDecimal number, int scale, int roundingMode) {
        if (number.compareTo(BigDecimal.ZERO) < 0)
            throw new ArithmeticException("sqrt with negative");
        BigInteger integer = number.toBigInteger();
        StringBuffer sb = new StringBuffer();
        String strInt = integer.toString();
        int lenInt = strInt.length();
        if (lenInt % 2 != 0) {
            strInt = '0' + strInt;
            lenInt++;
        }
        BigInteger res = BigInteger.ZERO;
        BigInteger rem = BigInteger.ZERO;
        for (int i = 0; i < lenInt / 2; i++) {
            res = res.multiply(BigInteger.TEN);
            rem = rem.multiply(BigInteger.valueOf(100));

            BigInteger temp = new BigInteger(strInt.substring(i * 2, i * 2 + 2));
            rem = rem.add(temp);

            BigInteger j = BigInteger.TEN;
            while (j.compareTo(BigInteger.ZERO) > 0) {
                j = j.subtract(BigInteger.ONE);
                if (((res.add(j)).multiply(j)).compareTo(rem) <= 0) {
                    break;
                }
            }

            res = res.add(j);
            rem = rem.subtract(res.multiply(j));
            res = res.add(j);
            sb.append(j);
        }
        sb.append('.');
        BigDecimal fraction = number.subtract(number.setScale(0, BigDecimal.ROUND_DOWN));
        int fracLen = (fraction.scale() + 1) / 2;
        fraction = fraction.movePointRight(fracLen * 2);
        String strFrac = fraction.toPlainString();
        for (int i = 0; i <= scale; i++) {
            res = res.multiply(BigInteger.TEN);
            rem = rem.multiply(BigInteger.valueOf(100));

            if (i < fracLen) {
                BigInteger temp = new BigInteger(strFrac.substring(i * 2, i * 2 + 2));
                rem = rem.add(temp);
            }

            BigInteger j = BigInteger.TEN;
            while (j.compareTo(BigInteger.ZERO) > 0) {
                j = j.subtract(BigInteger.ONE);
                if (((res.add(j)).multiply(j)).compareTo(rem) <= 0) {
                    break;
                }
            }
            res = res.add(j);
            rem = rem.subtract(res.multiply(j));
            res = res.add(j);
            sb.append(j);
        }
        return new BigDecimal(sb.toString()).setScale(scale, roundingMode);
    }
}
